package com.core.image.core.impl;

import com.core.image.core.Connector;

import java.util.concurrent.TimeUnit;

/**
 * describe:
 *
 * @author 库琦
 * @date 2020/02/05/22:22
 */
public class IdleTimeoutScheduleJob extends ScheduleJob {

    public IdleTimeoutScheduleJob(long idleTime, TimeUnit timeUnit, Connector connector) {
        super(idleTime, timeUnit, connector);
    }

    @Override
    public void run() {
        long lastAcTiveTime =connector.getLastActiveTime();
        //超时的最大时间
        long  idleTimeMilliseconds = this.idleTimeMilliseconds;
        long nextTime = idleTimeMilliseconds - (System.currentTimeMillis() - lastAcTiveTime);
        if (nextTime <= 0){
            schedule(idleTimeMilliseconds);
            try {
                connector.fireIdleTimeoutEvent();
            }catch (Throwable throwable){
                connector.fireExceptionCaught(throwable);
            }
        }else {
            // 激活时，如果当前判断未超时，则基于最后一次活跃时间进行二次调度
            schedule(nextTime);
        }

    }
}
